package com.xiaowen.controller;

import com.xiaowen.entity.Travel;
import com.xiaowen.service.CommentService;
import com.xiaowen.service.TravelService;
import com.xiaowen.util.CorrectJsonObject;
import com.xiaowen.util.ErrorJsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by 15c on 2017/11/18.
 */
@Controller
@RequestMapping("/travels")
public class TravelController {
    @Autowired
    TravelService travelService;
    @Autowired
    CommentService commentService;


    /**
     * 用于发布任务(必须给与创建者id与typeId，不然查询时结果为空值)
     *
     * @param travel 游记对象
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    String createNewTravels(@RequestBody(required = false) Travel travel) {
        //游记必须有标题，并且确保creator是登录者不为空，当然类型也最好不为空；前端进行判断
        ErrorJsonObject errorJsonObject;
        int i = travelService.addTravels(travel);
        if (1 != i) {
            errorJsonObject = new ErrorJsonObject(110, "游记发表失败！");
            return errorJsonObject.toJSONString();
        }
        CorrectJsonObject correctJsonObject = new CorrectJsonObject("游记发表成功！");
        correctJsonObject.setData(0);
        return correctJsonObject.toString();
    }

    /**
     * 通过travelId删除用户历史游记
     */
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    String deleteTravels(@RequestParam(required = false) Integer travelId) {
        //删除游记对应删除评论（后面实现）
        ErrorJsonObject errorJsonObject;
        //对应删除评论
        commentService.deleteListComments(travelId);
        int i = travelService.deleteTravels(travelId);
        if (1 != i) {
            errorJsonObject = new ErrorJsonObject(111, "游记删除失败！");
            return errorJsonObject.toJSONString();
        }

        CorrectJsonObject correctJsonObject = new CorrectJsonObject("游记删除成功！");
        correctJsonObject.setData(0);
        return correctJsonObject.toString();
    }

    /**
     * 通过travelId查询一条游记信息不包含评论
     *
     * @param travelId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/travelInfo", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    String showOneTravelsDetailsInfo(@RequestParam(required = false) Integer travelId) {
        ErrorJsonObject errorJsonObject;
        Travel travel;
        travel = travelService.showTravels(travelId);
        if (null == travel) {
            errorJsonObject = new ErrorJsonObject(112, "游记查询失败！");
            return errorJsonObject.toJSONString();
        }
        CorrectJsonObject correctJsonObject = new CorrectJsonObject("游记查询成功！");
        correctJsonObject.setData(travel);
        return correctJsonObject.toString();
    }

    /**
     * 通过userId查询一个用户所有游记;可以用于查自己所发全部游记，也可以查询他人
     *
     * @param userId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/listTravels", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    String showListTravels(@RequestParam(required = false) Integer userId) {
        ErrorJsonObject errorJsonObject;
        List<Travel> travelList;
        travelList = travelService.showListTravels(userId);
        if (travelList.isEmpty()) {
            errorJsonObject = new ErrorJsonObject(112, "游记查询为空！");
            return errorJsonObject.toJSONString();
        }
        CorrectJsonObject correctJsonObject = new CorrectJsonObject("游记查询成功！");
        correctJsonObject.setData(travelList);
        return correctJsonObject.toString();
    }

    /**
     * 游记点赞
     *
     * @param travelId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/expressingLove", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    String showTravelsLoving(@RequestParam(required = false) Integer travelId) {
        ErrorJsonObject errorJsonObject;
            int i = travelService.updateLikeNum(travelId);
            if (1 != i) {
                errorJsonObject = new ErrorJsonObject(113, "点赞失败！");
                return errorJsonObject.toJSONString();
            }
            CorrectJsonObject correctJsonObject = new CorrectJsonObject("点赞成功！");
            correctJsonObject.setData(0);
            return correctJsonObject.toString();
    }


    /**
     * 游记取消点赞
     *
     * @param travelId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/removingLove", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    String removeTravelsLoving(@RequestParam(required = false) Integer travelId) {
        //需要点赞用户才可取消点赞
        ErrorJsonObject errorJsonObject;
        int i = travelService.updateLikeNumLess(travelId);
        if (1 != i) {
            errorJsonObject = new ErrorJsonObject(113, "取消点赞失败！");
            return errorJsonObject.toJSONString();
        }
        CorrectJsonObject correctJsonObject = new CorrectJsonObject("取消点赞成功！");
        correctJsonObject.setData(0);
        return correctJsonObject.toString();
    }

    /**
     * 获得最新五条攻略类型游记
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getNewStrategy", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    String showNewStrategy() {
        //需要点赞用户才可取消点赞
        ErrorJsonObject errorJsonObject;
        List<Travel> travelList=travelService.showNewStrategy();
        if (travelList.isEmpty()) {
            errorJsonObject = new ErrorJsonObject(112, "攻略类型游记查询为空！");
            return errorJsonObject.toJSONString();
        }
        CorrectJsonObject correctJsonObject = new CorrectJsonObject("攻略类型游记查询成功！");
        correctJsonObject.setData(travelList);
        return correctJsonObject.toString();
    }
    /**
     * 获得最新五条攻略类型游记
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getNewTravel", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    String showNewTravel() {
        //需要点赞用户才可取消点赞
        ErrorJsonObject errorJsonObject;
        List<Travel> travelList=travelService.showNewTravel();
        if (travelList.isEmpty()) {
            errorJsonObject = new ErrorJsonObject(113, "其他类型游记查询为空！");
            return errorJsonObject.toJSONString();
        }
        CorrectJsonObject correctJsonObject = new CorrectJsonObject("其他类型游记查询成功！");
        correctJsonObject.setData(travelList);
        return correctJsonObject.toString();
    }
    /**
     * 获得最新4条点赞最多的攻略类型游记
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getManyLikeStrategy", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    String showManyLikeStrategy() {
        //需要点赞用户才可取消点赞
        ErrorJsonObject errorJsonObject;
        List<Travel> travelList=travelService.showManyLikeStrategy();
        if (travelList.isEmpty()) {
            errorJsonObject = new ErrorJsonObject(114, "点赞最多的攻略类型游记查询为空！");
            return errorJsonObject.toJSONString();
        }
        CorrectJsonObject correctJsonObject = new CorrectJsonObject("点赞最多的攻略类型游记查询成功！");
        correctJsonObject.setData(travelList);
        return correctJsonObject.toString();
    }
    /**
     * 获得最新4条点赞最多的其他类型游记
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getManyLikeTravel", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    String showManyLikeTravel() {
        //需要点赞用户才可取消点赞
        ErrorJsonObject errorJsonObject;
        List<Travel> travelList=travelService.showManyLikeTravel();
        if (travelList.isEmpty()) {
            errorJsonObject = new ErrorJsonObject(115, "点赞最多的其他类型游记查询为空！");
            return errorJsonObject.toJSONString();
        }
        CorrectJsonObject correctJsonObject = new CorrectJsonObject("点赞最多的其他类型游记查询成功！");
        correctJsonObject.setData(travelList);
        return correctJsonObject.toString();
    }
}
